# -*- coding: utf-8 -*-
import json
import re
import sys

reStr_phase4="DuerOS_VrApk_AsrEngine.*asrEventListener.*partial.*final_result"
reStr_jili="SpeechCallback.*partial.*final_result"

def read_file(filepath):
    print("%36s sn %19s corpus_no %1s engineType  asr %s" % ("", "", "", ""))
    with open(filepath, 'rb') as file:
        last_sn = ""
        while True:
            # 按行读取
            line = str(file.readline().decode('utf-8', errors='ignore')).strip()
            if line:
                # 多虑
                if re.search(reStr_jili, line):
                    # print(line)
                    start = line.index('{')
                    end = line.rfind('}')
                    # 截取json
                    if start != -1 and end != -1:
                        # print(line[start:end + 1])
                        json_obj = json.loads(line[start:end + 1])
                        asr = json_obj["results_recognition"][0]
                        sn = json_obj["sn"]
                        engineType = json_obj["engineType"]
                        if engineType == 0:
                            corpus_no = json_obj["origin_result"]["corpus_no"]
                        else:
                            corpus_no = "none"
                        if last_sn != sn:
                            print("-----------------------------------------------------------------------------------")
                            print("%39s %29s %12s  %s" % (sn, corpus_no, engineType, asr))
                        else:
                            print("%39s %29s %12s  %s" % (sn, corpus_no, engineType, asr))
                            print("-----------------------------------------------------------------------------------")
                        last_sn = sn
            else:
                break


if __name__ == "__main__":
    read_file(sys.argv[1])
